package MonteCarloMultiThread;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;

import View.MonteCarloGUI;
import View.Updatable;

public class MonteCarloSteps implements Callable<Double>{

    
	
	private double S,Drift,vSqrdt,X;
	int nSteps,z;
	Lock lockOption;
	Random random;
	MonteCarloSimulation simulation;
	Updatable viewToUpdate;
	
	
	
	public MonteCarloSteps(Updatable viewToUpdate,MonteCarloSimulation simulation,double s, double drift, double vSqrdt,
			double X, int nSteps, int z) {
		super();
		S = s;
		Drift = drift;
		this.vSqrdt = vSqrdt;
		this.nSteps = nSteps;
		this.z = z;
		this.X=X;
		this.simulation=simulation;
		this.viewToUpdate=viewToUpdate;
		
	}



	@Override
	public Double call() {
		
		random = new Random(5);
		double St=S;
		
		for (int j=0;j<nSteps;j++){
			St = St * java.lang.Math.exp(Drift + vSqrdt * random.nextGaussian());
			
		}
		
		simulation.addThreadID((int) Thread.currentThread().getId());
		simulation.addOneSimulation();
		double nbSimu = simulation.getNbSimulation();
		double nbSimuDone = simulation.getNbSimulationDone();
		int progress = (int) (nbSimuDone/nbSimu*100);
		//System.out.println(progress);
		viewToUpdate.setProgressBar(progress);
		
		return Math.max(z * (St-X), 0);
	}
	
	
}
